package com.elegantsoft.pakistannews;

import java.util.ArrayList;
import java.util.Arrays;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.atiffarrukh.pakistannews.R;
import com.elegantsoft.pakistannews.rssfeed.MessageList;
import com.google.analytics.tracking.android.EasyTracker;

public class AlternateSend extends SherlockActivity {
	String chan;
	Bundle extras;
	private boolean english;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
		extras = getIntent().getExtras();
		chan = extras.getString("channelName");
		
		Log.d("Language is English", ""+ extras.getBoolean("english"));
		english = extras.getBoolean("english");
		if (extras.getBoolean("english")) {
			if (chan.equals("Geo")) {
				String[] category = { "Headlines", "Pakistan", "World",
						"Buisness", "Entertainment", "Health" };
				listViewPopulate(category);
			} else if (chan.equals("Dawn")) {
				String[] category = { "Headlines", "Pakistan" };
				listViewPopulate(category);
			} else if (chan.equals("Express Tribune")) {
				String[] category = { "Headlines", "Pakistan", "World",
						"Buisness", "Entertainment", "Sports" };
				listViewPopulate(category);
			} else if (chan.equals("PTV News")) {
				String[] category = { "Headlines", "Pakistan" };
				listViewPopulate(category);
			} else if (chan.equals("Aaj News")) {
				String[] category = { "Headlines", "Pakistan" };
				listViewPopulate(category);
			} else if (chan.equals("The News")) {
				String[] category = { "Headlines", "Pakistan", "World",
						"Sports", "Buisness" };
				listViewPopulate(category);
			} else if (chan.equals("Popular Science")) {
				String[] category = { "Technology", "Science" };
				listViewPopulate(category);
			} else if (chan.equals("Pro Pakistani")) {
				String[] category = { "Technology" };
				listViewPopulate(category);
			} else if (chan.equals("BBC")) {
				String[] category = { "Technology","Science" };
				listViewPopulate(category);
			} else if (chan.equals("Buisness Recorder")) {
				String[] category = { "Buisness" };
				listViewPopulate(category);
			}
		} else {
			if (chan.equals("Jang News Urdu")) {
				String[] category = { "Headlines", "Pakistan", "World",
						"Buisness", "Sports" };
				listViewPopulate(category);
			} else if (chan.equals("Waqt")) {
				String[] category = { "Headlines", "Pakistan" };
				listViewPopulate(category);
			}  else if (chan.equals("Nawai Waqt Urdu")) {
				String[] category = { "Headlines", "Pakistan" };
				listViewPopulate(category);
			} 
		}

	}

	private void listViewPopulate(String[] category) {
		ListView lv = (ListView) findViewById(R.id.catList);

		ArrayList<String> categoryList = new ArrayList<String>();
		categoryList.addAll(Arrays.asList(category));

		MyCustommAdapter adapter = new MyCustommAdapter(this, category);

		lv.setAdapter(adapter);

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> paren, View view,
					int position, long id) {
				// TODO Auto-generated method stub

				if (isConnected()) {
					TextView tv = (TextView) view.findViewById(R.id.text1);
					String categoryString = tv.getText().toString();
					Intent i = new Intent(getApplicationContext(),
							MessageList.class);
					i.putExtra("category", categoryString);
					i.putExtra("channelName", chan);
					i.putExtra("alternate", true);
					i.putExtra("english", english);
					startActivity(i);
				} else
					Toast.makeText(getApplicationContext(),
							Farsi.Convert("You are not connected to Internet"),
							Toast.LENGTH_SHORT).show();
			}
		});
	}

	// Check the connectivity with Internet
	boolean isConnected() {
		ConnectivityManager conMgr = (ConnectivityManager) this
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo i = conMgr.getActiveNetworkInfo();
		if (i == null)
			return false;
		if (!i.isConnected())
			return false;
		if (!i.isAvailable())
			return false;
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case android.R.id.home:
			// Do whatever you want, e.g. finish()
			Intent i = new Intent(getApplicationContext(), AlternateStart.class);
			i.putExtra("english", extras.getBoolean("english"));
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			finish();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	 @Override
	  public void onStart() {
	    super.onStart();
	  
	    //EasyTracker.getInstance().activityStart(this); // Google Analytics.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    
	   // EasyTracker.getInstance().activityStop(this); // Google Analytics.
	  }
	

}
